package com.ujoin.service;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ujoin.bean.Activity;
import com.ujoin.bean.Broadcast;
import com.ujoin.bean.Follow;
import com.ujoin.bean.UserActivity;
import com.ujoin.dao.imp.ActivityDAO;
import com.ujoin.dao.imp.BroadcastDAO;
import com.ujoin.dao.imp.FollowDAO;
import com.ujoin.dao.imp.UserActivityDAO;
import com.ujoin.util.JsonUtil;

/**
 * 
 * @author Daniel
 * 待测试
 *
 */

public class BroadcastService {
	@Autowired
	private BroadcastDAO broadcastDao;
	
	@Autowired
	private FollowDAO followDAO;
	
	@Autowired
	private ActivityDAO activityDAO;
	
	@Autowired
	private UserActivityDAO userActivityDAO;
	
	/**
	 * 为公众活动创建广播
	 * @param ActivityId
	 * @return Json
	 */
	public String createBroadcast(int activityId){
		JsonUtil jo = new JsonUtil();
		Activity activity = activityDAO.findById(activityId);
		if (activity == null) {
			jo.getError();
			jo.setMessage("活动不存在，无法广播");
			return jo.toJSONString();
		}
		if (!activity.getValid()) {
			jo.getError();
			jo.setMessage("活动无效，无法广播");
		}
		List broadcastList = null;
		UserActivity userActivity = (UserActivity)userActivityDAO.findByActivityId(activityId).get(0);
		List followerList = followDAO.findByFolloweeId(userActivity.getUserId());
		Iterator<Follow> iterator = followerList.iterator();
		while(iterator.hasNext()) {
			Broadcast broadcast = new Broadcast();
			broadcast.setActicityId(activityId);
			broadcast.setUserId(iterator.next().getFollowerId());
			broadcast.setIsRead(false);
			broadcastDao.save(broadcast);
			broadcastList.add(broadcast);
		}
		jo.setComponentList(broadcastList).setSuccess();
		return jo.toJSONString();
	}
	
	/**
	 * 删除指定活动的广播
	 * @param activityId
	 * @return Json
	 */
    public String deleteBroadcast(int activityId){
    	JsonUtil jo = new JsonUtil();
		List broadcastList = broadcastDao.findByActicityId(activityId);
		if (broadcastList.isEmpty()) {
			jo.getError();
			jo.setMessage("活动不存在，无法删除广播");
			return jo.toJSONString();
		}
		Iterator<Broadcast> iterator = broadcastList.iterator();
		while (iterator.hasNext()) {
			broadcastDao.delete(iterator.next());
		}
		jo.getSuccess();
		return jo.toJSONString();
	}
    
    /**
     * 用户读完该广播后的处理（设为已读）
     * @param activityId
     * @param userId
     * @return
     */
    public String readBroadcast(int userId, int activityId) {
    	JsonUtil jo = new JsonUtil();
		Broadcast broadcast = broadcastDao.findByUserIdActivityId(userId,activityId);
		
		if (broadcast == null) {
			jo.getError();
			jo.setMessage("指定的记录不存在，无法更新广播为已读");
			return jo.toJSONString();
		}
		broadcast.setIsRead(true);
		broadcastDao.save(broadcast);
		jo.getSuccess();
		return jo.toJSONString();
    }
    
    /**
     * 找出某个用户接收的所有广播
     * @param userId
     * @return Json
     */
    public String findBroadcastByUserId(int userId){
    	List broadcastList = broadcastDao.findByUserId(userId);
    	JsonUtil jo = new JsonUtil();
    	jo.setComponentList(broadcastList);
    	jo.setSuccess();
    	return jo.toJSONString();
	}
    
    /**
     * 找出某个活动发出的所有广播
     * @param activityId
     * @return Json
     */
    public String findBroadcastByActivityId(int activityId){
    	List broadcastList = broadcastDao.findByActicityId(activityId);
    	JsonUtil jo = new JsonUtil();
    	jo.setComponentList(broadcastList);
    	jo.setSuccess();
    	return jo.toJSONString();
	}
    
    /**
     * 找出某个用户的未读广播
     * @param userId
     * @return Json
     */
    public String findUnreadBroadcastByUserId(int userId) {
    	JsonUtil jo = new JsonUtil();
    	List broadcastList = broadcastDao.findUnreadBroadcastByUserId(userId);
    	jo.setComponentList(broadcastList).setSuccess();
    	return jo.toJSONString();
    }
}
